#pragma once

#include <iostream>
#include <stdarg.h>
#include <unistd.h>
#include <time.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define Screen 1
#define OneFile 2
#define ClassFile 3

#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define SIZE 1024
#define LogFile "log.txt"

class Log
{
public:
    Log()
    {
        printMethod = Screen;
        path = "./log/";
    }

    // 将日志打印到Method
    void LogWhere(int Method)
    {
        printMethod = Method;
    }

    // 日志等级
    std::string LevelToString(int level)
    {
        switch (level)
        {
        case Info:
            return "Info";
            break;
        case Debug:
            return "Debug";
            break;
        case Warning:
            return "Warning";
            break;
        case Error:
            return "Error";
            break;
        case Fatal:
            return "Fatal";
            break;
        default:
            return "None";
            break;
        }
    }

    // void logmessage(int level, const char *format, ...)
    // {
    //     // 默认部分
    //     time_t t = time(nullptr);
    //     struct tm *ctime = localtime(&t);
    //     char leftbuffer[SIZE];
    //     snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]",
    //              ctime->tm_year + 1990, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour,
    //              ctime->tm_min, ctime->tm_sec);
    //     // 自定义部分
    //     va_list s;
    //     va_start(s, format);
    //     char rightbuffer[SIZE];
    //     vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
    //     va_end(s);

    //     // 合并格式：默认部分+自定义部分
    //     char logtxt[SIZE * 2];
    //     snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);
    //     printLog(level, logtxt); // 将日志打印到指定地方
    // }

    void printLog(int level, const std::string &logtxt)
    {
        // std::cout << "1" << std::endl;
        switch (printMethod)
        {
        case Screen:
            std::cout << logtxt << std::endl;
            break;
        case OneFile:
            PrintOneFile(LogFile, logtxt);
            break;
        case ClassFile:
            PrintClassFile(level, logtxt);
            break;
        default:
            break;
        }
    }

    void PrintOneFile(const std::string &logname, const std::string &logtxt)
    {
        std::string logname_ = path + logname;
        int fd = open(logname_.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        if (fd < 0)
            return;
        write(fd, logtxt.c_str(), logtxt.size());
        close(fd);
    }

    void PrintClassFile(int level, const std::string &logtxt)
    {
        std::string filename_ = LogFile;
        filename_ += ".";
        filename_ += LevelToString(level);
        PrintOneFile(filename_, logtxt);
    }

    void operator()(int level, const char *format, ...)
    {
        // 默认部分
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftbuffer[SIZE];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]",LevelToString(level).c_str(),
                 ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday, ctime->tm_hour,
                 ctime->tm_min, ctime->tm_sec);
        // 自定义部分
        va_list s;
        va_start(s, format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);

        // 合并格式：默认部分+自定义部分
        char logtxt[SIZE * 2];
        snprintf(logtxt, sizeof(logtxt), "%s %s\n", leftbuffer, rightbuffer);
        
        printLog(level, logtxt); // 将日志打印到指定地方
        // printf("%s", logtxt);
    }

    ~Log()
    {}
private:
    int printMethod;
    std::string path;
};

// int sum(int n, ...)
// {
//     va_list s; // char*
//     va_start(s, n);

//     int sum = 0;
//     while(n)
//     {
//         sum += va_arg(s, int); // printf("hello %d, hello %s, hello %c, hello %d,", 1, "hello", 'c', 123);
//         n--;
//     }

//     va_end(s); //s = NULL
//     return sum;
// }
